package screen.window;

import globals.Fonts;
import globals.Global;

import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.util.ArrayList;

import screen.item.TextButton;

public class MenuWindow extends InterfaceWindow {
	
	public MenuWindow() {
		windowButtons = new ArrayList<TextButton>();
		HEIGHT = 32 * Global.Instance().tileSize + 2 * Global.Instance().INTERFACE_BORDER;
		WIDTH  = 16 * Global.Instance().tileSize + 2 * Global.Instance().INTERFACE_BORDER;
	}
	
	public MenuWindow(int x, int y) {
		this();
		windowX = x;
		windowY = y;
	}

	public void show() { windowShow = true;}
	
	public void Load() {
		int xOffset = 55;
		int yOffset = 400;
		int shift = 32;
		
		windowButtons.add(new TextButton("Inventory", xOffset, yOffset, new Color(0.2f,0.2f,0.2f,1.0f), Fonts.menu, Fonts.menuMetrics) {
			@Override public void mouseClick() {
			}
			@Override public void mouseOver() {
				// TODO Auto-generated method stub
			}});
		
		windowButtons.add(new TextButton("Exit", xOffset, yOffset + shift, new Color(0.2f,0.2f,0.2f,1.0f), Fonts.menu, Fonts.menuMetrics) {
			@Override public void mouseClick() {
				windowShow = false;
				Global.Instance().networkManager.disconnect();
			}
			@Override public void mouseOver() {
				// TODO Auto-generated method stub
			}});
	}
	
	@Override
	public void Update(float elapsed) {
		if (windowShow) {
			// Fly down
			if (windowZoom < Global.Instance().winHeight) windowZoom += Global.Instance().winHeight/30.0f;
			// Fade in
			if (windowFade < 1.0) windowFade += 0.02f;
			if (windowFade > 1.0) windowFade = 1.0f;
			
			// Update buttons
			for (TextButton b : windowButtons) {
				b.Update(elapsed);
			}
			
			// Input
			if (Global.Instance().input.isKeyReleased(Event.ESCAPE)) {
				windowShow = false;
			}
		} else {
			// Fly up
			if (windowZoom > 0) windowZoom -= Global.Instance().winHeight/30.0f;
			// Fade out
			if (windowFade > 0) windowFade -= 0.02f;
			if (windowFade < 0) windowFade = 0f;
			
			if (Global.Instance().input.isKeyReleased(Event.ESCAPE))
				show();
		}
	}

	@Override
	public void Draw(Graphics graphics) {
		// Draw Backdrop
		Color c = new Color(0.1f,0.1f,0.1f,windowFade/2);
		graphics.setColor(c);
		graphics.fillRect(50, 0, 200, (int) windowZoom);
		// Draw buttons
        c = new Color(0.9f,0.9f,0.9f,windowFade);
        Color co = new Color(0.6f,0.6f,0.6f,windowFade);
        for (TextButton b : windowButtons) {
        	b.color = c;
        	b.mouseOverColor = co;
        	b.Draw(graphics);
        }
	}
}
